package com.xiaoxin.smartbi.mqtest.mq7;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.util.HashMap;
import java.util.Map;

public class ReceiveLogsDirect {

  private static final String EXCHANGE_NAME = "direct_copy_logs";

  private static final String dle="dle";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(EXCHANGE_NAME,"direct");

    String queueName1 = "小li";
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-dead-letter-exchange", dle);
    args.put("x-dead-letter-routing-key", "外包");
    channel.queueDeclare(queueName1, false, false, false, args);
    channel.queueBind(queueName1, EXCHANGE_NAME, queueName1);
    System.out.println(queueName1+"  Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback1 = (consumerTag, delivery) -> {
      String message = new String(delivery.getBody(), "UTF-8");
      channel.basicNack(delivery.getEnvelope().getDeliveryTag(),false,false);
      System.out.println(queueName1+"  Received '" +
            delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
    };
    channel.basicConsume(queueName1, false, deliverCallback1, consumerTag -> { });


    String queueName2 = "小wang";
    Map<String, Object> args2 = new HashMap<String, Object>();
    args2.put("x-dead-letter-exchange", dle);
    args2.put("x-dead-letter-routing-key", "老板");
    channel.queueDeclare(queueName2, false, false, false, args2);
    channel.queueBind(queueName2, EXCHANGE_NAME, queueName2);
    System.out.println(queueName2+"  Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback2 = (consumerTag, delivery) -> {
      String message = new String(delivery.getBody(), "UTF-8");
      channel.basicNack(delivery.getEnvelope().getDeliveryTag(),false,false);
      System.out.println(queueName2+"  Received '" +
              delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
    };
    channel.basicConsume(queueName2, false, deliverCallback2, consumerTag -> { });
  }
}